@isTest
private class NoContractInstrumentAppHandlerTest {
    static testMethod void testMethod1() {
        


         Account acc = TestUtils.createAccount();
        acc.Level__c = '三级甲等';
        acc.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('Dealer', 'Account');
         
        insert acc; 

        Contact ctt  = new Contact();
        ctt.AccountId = acc.Id;
        ctt.LastName = 'xx';
        ctt.Phone = '1234';
        //ctt.Post__c = '分管院长(设备)';
        //ctt.Departments__c = '医院办公室';
        insert ctt;


        //业务机会1
        Opportunity opp = TestUtils.createOpportunity();
        opp.AccountId = acc.Id;
        insert opp;

         Contract__c con = new Contract__c();
        con.Account__c = acc.Id;
        con.Opportunity__c = opp.Id;
        con.Status__c  = '已生效';
        con.Corpcode__c = 'MKSY';
        con.RecordTypeId = Utils.getRecordTypeIdByDevNameAndObjName('TerminalContract', 'Contract__c');
        insert con;


        Id pRT = Utils.getRecordTypeIdByDevNameAndObjName('NCProduct', 'Product__c');

        Product__c p  = TestUtils.createInst();
        p.Name = '测试仪器';
        p.RecordTypeId = pRT;
        p.Category0__c  = '仪器';
        p.ProductName__c = '测试仪器';
       // p.ProductStructure__c = ps1.Id;
       // p.CRMBrand__c = brand.Id;
        insert p ;


          //报价2
        Quote qt2 = new Quote(Name = '试剂报价',Type__c = '试剂报价',OpportunityId = opp.Id,IsActive__c = true);
        qt2.Status = '已批准';
        qt2.Contract__c = con.Id;
        insert qt2 ;

        QuoteProduct__c qp = new QuoteProduct__c(Name = '测试报价产品2',Product__c = p.Id,Quote__c = qt2.Id,SalesPrice__c = 5);
        insert qp ;


        ContractProduct__c cp = new ContractProduct__c();
        cp.Contract__c = con.Id;
        cp.Product__c =p.Id  ;
        cp.Quantity__c = 5;
        cp.OriginNumber__c = 5;
        cp.SalesPrice__c = 1; 
        cp.QuoteProduct__c = qp.Id;
        cp.ApplicationQuantity__c = 3;
        cp.Opportunity__c = opp.Id;
        insert cp ;


        //无销售合同
        NoContractInstrumentApplication__c noc =new NoContractInstrumentApplication__c();
        noc.Contract__c = con.Id;
        noc.stage__c = '草案中';
        insert noc;

        NoContractInstrumentApplicationDetail__c nocd = new NoContractInstrumentApplicationDetail__c();
        nocd.ApplicationInstrument__c =  cp.Id; 
        nocd.NoContractInstrumentApplication__c = noc.Id;
        nocd.Number__c = 1; 
        nocd.ExpectedProcessingTime__c = System.today().addDays(-1);
        nocd.ExpectedProcessingWay__c = '签合同';// = '不签合同';
        insert nocd ; 


        PageReference page =  new PageReference('/apex/NoContractInstrumentApplicationDetail?id=' +opp.Id);
        System.Test.setCurrentPage(page);

        NoContractApplicationDetailController  noaController = new NoContractApplicationDetailController();
        noaController.init();

        Map<String,Object> save1Map = new Map<String,Object>();
        save1Map.put('file', 'xxxx');
        save1Map.put('filename', '合同');
        save1Map.put('format', '.txt');
        save1Map.put('rename', '合同');

        noaController.saveJson = EncodingUtil.urlEncode(JSON.serialize(save1Map), 'UTF-8');
        noaController.upload();


        List<NoContractApplicationDetailController.ProductBean>  cpList =new List<NoContractApplicationDetailController.ProductBean>();
            NoContractApplicationDetailController.ProductBean cmb = new NoContractApplicationDetailController.ProductBean(cp);
           
        cpList.add(cmb);
        Map<String,Object> saveMap = new Map<String,Object>();
        saveMap.put('datas', cpList);

        Map<String,String> baseMap = new Map<String,String>();
        baseMap.put('Customer__c', acc.Id );
        baseMap.put('Contancts__c', ctt.Id );
        saveMap.put('base', baseMap);

        noaController.saveJson = EncodingUtil.urlEncode(JSON.serialize(saveMap), 'UTF-8');

        noaController.save();



         PageReference page1 =  new PageReference('/apex/NoContractInstrumentApplicationDetail?id=' +noc.Id);
        System.Test.setCurrentPage(page1);

        NoContractApplicationDetailController  noaController1 = new NoContractApplicationDetailController();
        noaController1.init();


        noc.stage__c = '已批准';
        update noc ;

System.debug('NoContractInstrumentApplicationDetail__c==>'+[select WhetherFinishe__c,ExpectedProcessingWay__c,InstrumentConfirmation__c from NoContractInstrumentApplicationDetail__c where Id =: nocd.Id ]);
        DataBase.executeBatch(new NoContractApplicationBatch(), 1); 


        new NoContractApplicationSecheduler().execute(null);


         PageReference page2 =  new PageReference('/apex/NoContractAttachment?id=' +nocd.Id+'&fileValue=agc,DDDDXXDDS&fileFormat=.txt&fileName=test');
        System.Test.setCurrentPage(page2);


        NoContractAttachmentController natc = new NoContractAttachmentController();
        natc.init();
        natc.save();




    }
}